package cc.admin.modules.graduate.job;

import cc.admin.common.util.RedisUtil;
import cc.admin.modules.graduate.entity.Forum;
import cc.admin.modules.graduate.entity.Topic;
import cc.admin.modules.graduate.service.IForumService;
import cc.admin.modules.graduate.service.IUserForumLikeService;
import cc.admin.modules.quartz.job.AbstractJob;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cc.admin.modules.graduate.controller.UserForumLikeController.parseStringToMap;

@Slf4j
public class ForumTopJob extends AbstractJob {

    @Autowired
    private IUserForumLikeService userForumLikeService;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IForumService forumService;

    @Override
    public void doExecute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        JSONObject jsonObject = userForumLikeService.countUserIdByForumId();
        JSONObject group_by_forum_id = jsonObject.getJSONObject("group_by_forum_id");
        JSONArray bucketsArr = group_by_forum_id.getJSONArray("buckets");
        ArrayList<Forum> forums = new ArrayList<>();
        bucketsArr.forEach(bucket -> {
            Map<String, Object> stringObjectMap = parseStringToMap(String.valueOf(bucket));
            Forum forum = forumService.getById((String) stringObjectMap.get("key"));
            String likeNum = stringObjectMap.get("doc_count").toString();
            forum.setLikeNum(likeNum);
            if (forum.getViedoUrlText() != null && !forum.getViedoUrlText().equals("")) {
                forum.setViedoUrls(Arrays.stream(forum.getViedoUrlText().split(",")).collect(Collectors.toList()));
            }
            LambdaQueryWrapper<Forum> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Forum::getParentId, forum.getId());
            lambdaQueryWrapper.isNull(Forum::getDiscussion);
            List<Forum> childlist = forumService.list(lambdaQueryWrapper);
            childlist.forEach(childForum -> {
                forumContent(childForum);
                LambdaQueryWrapper<Forum> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper2.eq(Forum::getParentId, childForum.getId());
                lambdaQueryWrapper2.isNotNull(Forum::getDiscussion);
                List<Forum> childlist2 = forumService.list(lambdaQueryWrapper2);
                childlist2.forEach(childForum2->{
                    forumContent(childForum2);
                    LambdaQueryWrapper<Forum> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper3.eq(Forum::getParentId, childForum2.getId());
                    lambdaQueryWrapper3.isNotNull(Forum::getDiscussion);
                    List<Forum> childlist3 = forumService.list(lambdaQueryWrapper3);
                    childForum2.setChildForums(childlist3);
                });
                childForum.setChildForums(childlist2);
            });
            forum.setChildForums(childlist);
            forums.add(forum);
        });
        redisUtil.set("countUserIdByForumId",forums);
        log.info("ForumTopJob执行完毕");
    }

    public void forumContent(Forum forum) {
        String topicLabel = forum.getTopicLabel();
        String topicValue = forum.getTopicValue();
        Topic topic = new Topic(topicLabel, topicValue);
        forum.setTopic(topic);
        if (forum.getViedoUrlText() != null && forum.getViedoUrlText().split(",") != null) {
            List<String> videoUrls = Arrays.stream(forum.getViedoUrlText().split(",")).collect(Collectors.toList());
            forum.setViedoUrls(videoUrls);
        }
    }
}
